import requests
from bs4 import BeautifulSoup
import os

url = 'http://localhost:9989/#/chart/preview/19'  # 替换为您要爬取的网页的URL

response = requests.get(url)
content = response.text

# 创建保存文件的文件夹
folder = 'webpage'
if not os.path.exists(folder):
    os.makedirs(folder)

# 保存网页的HTML
with open(os.path.join(folder, 'index.html'), 'w', encoding='utf-8') as file:
    file.write(content)

# 解析网页内容
soup = BeautifulSoup(content, 'html.parser')

# 爬取并保存网页的JS和CSS文件
print('soup:',soup)
for script in soup.find_all('script'):
    src = script.get('src')
    
    if src and (src.endswith('.js') or src.endswith('.css')):
        filename = os.path.join(folder, src.split('/')[-1])
        with open(filename, 'wb') as file:
             if src is not None:
                src = src[1:]
                src = 'http://localhost:9989' + src  
                print('src:',src)
                file.write(requests.get(src).content)

# 爬取并保存网页的图片
for img in soup.find_all('img'):
    src = img.get('src')
    if src and (src.endswith('.jpg') or src.endswith('.png') or src.endswith('.gif')):
        filename = os.path.join(folder, src.split('/')[-1])
        with open(filename, 'wb') as file:
            file.write(requests.get(src).content)

print('网页的HTML、JS、CSS和图片已爬取并保存到webpage文件夹中。')
